
package com.example.four; // 与项目根包名一致
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.four.HistoryRecord;

import java.util.ArrayList;
import java.util.List;

/**
 * SQLite数据库助手类
 * 管理历史占卜记录的创建、插入、查询
 */
public class DivinationDatabaseHelper extends SQLiteOpenHelper {
    // 数据库信息
    private static final String DB_NAME = "MoneyDivinationDB";
    private static final int DB_VERSION = 1;
    // 历史记录表
    private static final String TABLE_HISTORY = "history_records";
    // 表字段
    private static final String COLUMN_ID = "_id";
    private static final String COLUMN_TIME = "divination_time"; // 占卜时间（毫秒值）
    private static final String COLUMN_QUESTION = "question"; // 问事内容
    private static final String COLUMN_HEXAGRAM_NAME = "hexagram_name"; // 卦名
    private static final String COLUMN_SUMMARY = "divination_summary"; // 解卦摘要

    public DivinationDatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    /**
     * 创建数据库表
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTableSql = "CREATE TABLE " + TABLE_HISTORY + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_TIME + " LONG NOT NULL, " +
                COLUMN_QUESTION + " TEXT, " +
                COLUMN_HEXAGRAM_NAME + " TEXT NOT NULL, " +
                COLUMN_SUMMARY + " TEXT NOT NULL)";
        db.execSQL(createTableSql);
    }

    /**
     * 数据库版本更新（v1.0暂无需处理）
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 如需更新，可在此处添加表结构修改逻辑
    }

    /**
     * 插入历史记录
     */
    public long insertHistoryRecord(HistoryRecord record) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_TIME, record.getDivinationTime());
        values.put(COLUMN_QUESTION, record.getQuestion());
        values.put(COLUMN_HEXAGRAM_NAME, record.getHexagramName());
        values.put(COLUMN_SUMMARY, record.getDivinationSummary());
        // 插入数据并返回记录ID
        long id = db.insert(TABLE_HISTORY, null, values);
        db.close();
        return id;
    }

    /**
     * 查询所有历史记录（按时间倒序）
     */
    public List<HistoryRecord> queryAllHistoryRecords() {
        List<HistoryRecord> records = new ArrayList<>();
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(
                TABLE_HISTORY,
                null,
                null,
                null,
                null,
                null,
                COLUMN_TIME + " DESC" // 按时间倒序，最新记录在前
        );

        if (cursor.moveToFirst()) {
            do {
                HistoryRecord record = new HistoryRecord();
                record.setId(cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_ID)));
                record.setDivinationTime(cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_TIME)));
                record.setQuestion(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_QUESTION)));
                record.setHexagramName(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_HEXAGRAM_NAME)));
                record.setDivinationSummary(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_SUMMARY)));
                records.add(record);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return records;
    }
}